package Gui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class PlayerNamesFrame extends JFrame implements ActionListener , PropertyChangeListener {

	private JScrollPane scrollPane;
	private JPanel contentPane;
	private  JButton btnStart;
	private BackgroundPanel panel;
	private JTable table;
	private DefaultTableModel model;
	private JButton btnAddPlayer;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PlayerNamesFrame frame = new PlayerNamesFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void actionPerformed(ActionEvent evt) {
		if (evt.getActionCommand().equals("start")){
			int count = App.getNumOfPlayers();
			Player[] players = new Player[count];
			for (int i = 0 ; i < count ; i++){
				players[i] = App.getPlayer(i);
			}
			App.readytoplay(players, count);
			setVisible(false);
		}
		if (evt.getActionCommand().equals("add-player")){
			addPlayer addplayer = new addPlayer();
			addplayer.setVisible(true);
			setVisible(false);
		}
	}

	/**
	 * Create the frame.
	 */
	public PlayerNamesFrame() {

		setResizable(false);
		setTitle("JOIN GAME");
		setBounds(100, 100, 646, 465);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 51, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		panel = new BackgroundPanel("/wheel_of_fortune_header.jpg",false);
		sl_contentPane.putConstraint(SpringLayout.NORTH, panel, 0, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, panel, 0, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, panel, 0, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, panel, 0, SpringLayout.EAST, contentPane);
		contentPane.add(panel);

		btnStart = new JButton("Start Game");
		btnStart.setVisible(false);
		btnStart.addActionListener(this);
		btnStart.setActionCommand("start");
		btnStart.setFont(new Font("Tahoma", Font.PLAIN, 17));
		sl_contentPane.putConstraint(SpringLayout.NORTH, btnStart, 228, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, btnStart, 46, SpringLayout.WEST, contentPane);

		scrollPane = new JScrollPane();
		
		btnAddPlayer = new JButton("Add another player");
		btnAddPlayer.setFont(new Font("Tahoma", Font.PLAIN, 12));
		btnAddPlayer.addActionListener(this);
		btnAddPlayer.setActionCommand("add-player");

		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(237)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(btnAddPlayer, GroupLayout.PREFERRED_SIZE, 142, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(btnStart, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE))
							.addContainerGap(251, Short.MAX_VALUE))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
					.addGap(73)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
					.addGap(23)
					.addComponent(btnStart, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(btnAddPlayer)
					.addContainerGap(70, Short.MAX_VALUE))
		);


		model=new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
						"Player"
				}
		){
			Class[] columnTypes = new Class[] {
					String.class, Integer.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
					false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		};
		table = new JTable(model);
		scrollPane.setViewportView(table);
		panel.setLayout(gl_panel);
		table.addMouseListener(new MouseListener() {


			@Override
			public void mousePressed(MouseEvent arg0) {

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				//
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void addUserToTable(String name){		
		model.addRow(new Object[]{name});
		btnStart.setVisible(true);
	}
	
	public void clearUserTable(){
		int count = model.getRowCount();
		for (int i = 0 ; i < count ; i++){
			model.removeRow(0);
		}
	}
	
	public int getNumRows(){
		return model.getRowCount();
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		//
	}
	@Override
	public void dispose() {		
		super.dispose();

	}
}